﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using SaunaServer.Model.Database;

namespace SaunaServer.Model.Network
{
    [Serializable]
    public class NetworkSetting
    {
        private FtpBaseSetting ftpBaseSetting;
        private TcpBaseSetting tcpBaseSetting;
        private DBBaseSetting dbBaseSetting;
        private string logFileDirectory;

        private const string DEFAULT_FTP_HOST_ADDRESS = "127.0.0.1";
        private const int DEFAULT_FTP_PORT_NUMBER = 21;
        private const bool DEFAULT_FTP_USE_SSL = true;
        private const string DEFAULT_FTP_USER_NAME = "SaunaFtpUser";
        private const string DEFAULT_FTP_PASSWORD = "pass333";
        private const string DEFAULT_FTP_SERVER_DIRECTORY = "/";
        private const string DEFAULT_FTP_LOCAL_DIRECTORY = @"c:\work\";

        private const string DEFAULT_TCP_HOST_ADDRESS = "127.0.0.1";
        private const int DEFAULT_TCP_PORT_NUMBER = 1723;
        private const bool DEFAULT_TCP_USE_SSL = true;

        private const string DEFAULT_DB_NAME = "sauna";
        private const string DEFAULT_DB_HOST_ADDRESS = "127.0.0.1";
        private const string DEFAULT_DB_USER_NAME = "root";
        private const string DEFAULT_DB_PASSWORD = "pass";

        private const string DEFAULT_LOG_FILE_DIRECTORY = @"\";

        private const string FILE_LOGGER = "FileLogger";
        private const string SFTP_LOG_MESSAGE = "Command: {0}";

        /// <summary>
        /// Default Constructor
        /// </summary>
        public NetworkSetting()
        {
            ftpBaseSetting = new FtpBaseSetting(DEFAULT_FTP_HOST_ADDRESS, DEFAULT_FTP_PORT_NUMBER, DEFAULT_FTP_USE_SSL,
                                                DEFAULT_FTP_USER_NAME, DEFAULT_FTP_PASSWORD, DEFAULT_FTP_SERVER_DIRECTORY,
                                                DEFAULT_FTP_LOCAL_DIRECTORY, DEFAULT_LOG_FILE_DIRECTORY);

            tcpBaseSetting = new TcpBaseSetting(DEFAULT_TCP_HOST_ADDRESS, DEFAULT_TCP_PORT_NUMBER, DEFAULT_TCP_USE_SSL, 
                                                DEFAULT_LOG_FILE_DIRECTORY);

            dbBaseSetting = new DBBaseSetting(DEFAULT_DB_NAME, DEFAULT_DB_HOST_ADDRESS, DEFAULT_DB_USER_NAME, DEFAULT_DB_PASSWORD,
                                                DEFAULT_LOG_FILE_DIRECTORY);

            this.logFileDirectory = DEFAULT_LOG_FILE_DIRECTORY;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public NetworkSetting(string ftpHostAddress, string ftpUserName, string ftpPassword,
                              string ftpServerDirectory, string ftpLocalDirectory, bool ftpSsl,
                              int ftpPortNumber, string tcpHostAddress, int tcpPortNumber, bool tcpSsl,
                              string dbDatabaseName, string dbHostAddress, string dbUserName, string dbPassword, string logFileDirectory)
        {
            ftpBaseSetting = new FtpBaseSetting(ftpHostAddress, ftpPortNumber, ftpSsl, ftpUserName, 
                                                ftpPassword, ftpServerDirectory, ftpLocalDirectory, logFileDirectory);

            tcpBaseSetting = new TcpBaseSetting(tcpHostAddress, tcpPortNumber, tcpSsl, logFileDirectory);

            dbBaseSetting = new DBBaseSetting(dbDatabaseName, dbHostAddress, dbUserName, dbPassword, logFileDirectory);

            this.logFileDirectory = logFileDirectory;
        }

        public NetworkSetting Clone()
        {
            NetworkSetting destinationNetworkSetting = new NetworkSetting();

            destinationNetworkSetting = new NetworkSetting(FtpHostAddress, FtpUserName, FtpPassword, FtpServerDirectory,
                                                           FtpLocalDirectory, FtpSsl, FtpPortNumber, TcpHostAddress,
                                                           TcpPortNumber, TcpSsl, DBDatabaseName, DBHostAddress, DBUserName, 
                                                           DBPassword, LogFileDirectory);
            return destinationNetworkSetting;
        }

        public void Copy(NetworkSetting sourceNetworkSetting)
        {
            FtpHostAddress = sourceNetworkSetting.FtpHostAddress;
            FtpUserName = sourceNetworkSetting.FtpUserName;
            FtpPassword = sourceNetworkSetting.FtpPassword;
            FtpServerDirectory = sourceNetworkSetting.FtpServerDirectory;
            FtpLocalDirectory = sourceNetworkSetting.FtpLocalDirectory;
            FtpPortNumber = sourceNetworkSetting.FtpPortNumber;
            FtpSsl = sourceNetworkSetting.FtpSsl;
            TcpHostAddress = sourceNetworkSetting.TcpHostAddress;
            TcpPortNumber = sourceNetworkSetting.TcpPortNumber;
            TcpSsl = sourceNetworkSetting.TcpSsl;
            DBHostAddress = sourceNetworkSetting.DBHostAddress;
            DBUserName = sourceNetworkSetting.DBUserName;
            DBPassword = sourceNetworkSetting.DBPassword;
            LogFileDirectory = sourceNetworkSetting.LogFileDirectory;
        }

        public string FtpHostAddress
        {
            get { return ftpBaseSetting.HostAddress; }
            set { ftpBaseSetting.HostAddress = value; }
        }

        public string FtpUserName
        {
            get { return ftpBaseSetting.UserName; }
            set { ftpBaseSetting.UserName = value; }
        }

        public string FtpPassword
        {
            get { return ftpBaseSetting.Password; }
            set { ftpBaseSetting.Password = value; }
        }

        public string FtpServerDirectory
        {
            get { return ftpBaseSetting.ServerDirectory; }
            set { ftpBaseSetting.ServerDirectory = value; }
        }

        public string FtpLocalDirectory
        {
            get { return ftpBaseSetting.LocalDirectory; }
            set { ftpBaseSetting.LocalDirectory = value; }
        }

        public bool FtpSsl
        {
            get { return ftpBaseSetting.UseSsl; }
            set { ftpBaseSetting.UseSsl = value; }
        }

        public int FtpPortNumber
        {
            get { return ftpBaseSetting.PortNumber; }
            set { ftpBaseSetting.PortNumber = value; }
        }

        public string TcpHostAddress
        {
            get { return tcpBaseSetting.HostAddress; }
            set { tcpBaseSetting.HostAddress = value; }
        }

        public int TcpPortNumber
        {
            get { return tcpBaseSetting.PortNumber; }
            set { tcpBaseSetting.PortNumber = value; }
        }

        public bool TcpSsl
        {
            get { return tcpBaseSetting.UseSsl; }
            set { tcpBaseSetting.UseSsl = value; }
        }

        public string DBDatabaseName
        {
            get { return dbBaseSetting.DatabaseName; }
            set { dbBaseSetting.DatabaseName = value; }
        }

        public string DBHostAddress
        {
            get { return dbBaseSetting.HostAddress; }
            set { dbBaseSetting.HostAddress = value; }
        }

        public string DBUserName
        {
            get { return dbBaseSetting.UserName; }
            set { dbBaseSetting.UserName = value; }
        }

        public string DBPassword
        {
            get { return dbBaseSetting.Password; }
            set { dbBaseSetting.Password = value; }
        }

        public string LogFileDirectory
        {
            get { return logFileDirectory; }
            set
            {
                logFileDirectory = value;
                ftpBaseSetting.LogFileDirectory = value;
                tcpBaseSetting.LogFileDirectory = value;
            }
        }

    }
}
